package com.yvon.maple.service.auth.controller;

import com.yvon.boot.common.result.GlobalException;
import com.yvon.boot.common.result.ResponseData;
import com.yvon.boot.jwt.model.AuthInfo;
import com.yvon.maple.cloud.web.data.ApiController;
import com.yvon.maple.service.auth.authentication.granter.TokenGranterHolder;
import com.yvon.maple.service.auth.param.CodeParam;
import com.yvon.maple.service.auth.param.LoginParam;
import com.yvon.maple.service.auth.valicode.validate.code.processor.CodeProcessorHolder;
import com.yvon.maple.service.auth.valicode.vo.CodeVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/oauth")
@Api(value = "授权管理", tags = "授权服务 - 授权管理API")
public class AuthController extends ApiController {


    @Autowired
    private TokenGranterHolder tokenGranterHolder;

    @Autowired
    private CodeProcessorHolder codeProcessorHolder;


    @ApiOperation(value = "登录", notes = "登录")
    @PostMapping(value = "/login")
    public ResponseData<AuthInfo> login(@Validated @RequestBody LoginParam login) {
        return success(tokenGranterHolder.getGranter(login.getGrantType()).grant(login));
    }

    @ApiOperation(value = "登出", notes = "登出")
    @DeleteMapping(value = "/logout")
    public ResponseData logout()  {
        return success(null);
    }


    @ApiOperation(value = "获取验证码", notes = "获取验证码")
    @PostMapping(value = "/code")
    public ResponseData<CodeVo> code(@Validated @RequestBody CodeParam login) {
       return success(codeProcessorHolder.getProcessor(login.getValidateType()).create(login));
    }


}
